package com.hj.saapp.common.utils;

import android.util.SparseArray;


import com.hj.saapp.BCDApplication;

import java.io.File;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串操作工具包
 *
 * @author liux (http://my.oschina.net/liux)
 * @version 1.0
 * @created 2012-3-21
 */
public class StringUtils {


    public static String getStringById(int id) {
        return BCDApplication.getApp().getResources().getString(id);
    }




    public static String getInt(String str) {
        String str2 = "";
        if (str != null && !"".equals(str)) {
            for (int i = 0; i < str.length(); i++) {
                if (str.charAt(i) >= 48 && str.charAt(i) <= 57) {
                    str2 += str.charAt(i);
                }
            }
        }
        return str2;
    }



    public static boolean isWithdraw(String mobiles) {
        Pattern p = Pattern
                .compile("^(([1-9]+)|([0-9]+\\.[0-9]{1,2}))$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    public static String ToDBC(String input) {
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 12288) {
                c[i] = (char) 32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375)
                c[i] = (char) (c[i] - 65248);
        }
        return new String(c);
    }

    /**

     * 判定输入汉字

     *

     * @param c

     * @return

     */

    public static  boolean isChinese(char c) {

        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);

        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {

            return true;

        }

        return false;

    }



    /**

     * 检测String是否全是中文

     *

     * @param name

     * @return

     */

    public static  boolean checkNameChese(String name) {

        boolean res = true;

        char[] cTemp = name.toCharArray();

        for (int i = 0; i < name.length(); i++) {

            if (!isChinese(cTemp[i])) {

                res = false;

                break;

            }

        }
        return res;
    }
    public static boolean isSpecialCharacter(String str)
    {

        String regEx="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！ @#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
        Pattern   p   =   Pattern.compile(regEx);
        Matcher   m   =   p.matcher(str);
        return  m.find();
    }

    public static String ToSBC(String input) {
        char c[] = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == ' ') {
                c[i] = '\u3000';
            } else if (c[i] < '\177') {
                c[i] = (char) (c[i] + 65248);
            }
        }
        return new String(c);
    }

    public static ArrayList getsparseArrayData(SparseArray sparseArray,ArrayList mData)
    {
        for(int i = 0, nsize = sparseArray.size(); i < nsize; i++)
        {
            Object obj = sparseArray.valueAt(i);
            mData.add(obj);
        }
        return mData;
    }
    public static boolean isNullEntity(String string)
    {
        if (string == null)
        {
            return true;
        }
        if (("").equals(string))
        {
            return  true;
        }
        return  false;
    }



    /**
     * 把字符串的后n位用“*”号代替

     * @return
     */

    public static String replaceSubString(String s){
        String sub="";
        try {
            for (int i = 0; i < s.length(); i++) {

                String x = s.substring(i,i+1);
                if (4 < i && i< 9)
                {
                    x = "*";
                }
                sub += x;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sub;
    }

    public static boolean isNumeric(String str){
        for (int i = str.length();--i>=0;){
            if (!Character.isDigit(str.charAt(i))){
                return false;
            }
        }
        return true;
    }

    public static String getSDCardPath() {
        // 获取扩展存储设备的文件目录
        File SDFile = android.os.Environment
                .getExternalStorageDirectory();

        File myFile = new File(SDFile.getAbsolutePath()
                + File.separator + "HJAPP");
        if (!myFile.exists()) {
            myFile.mkdirs();
        }
        return myFile.getAbsolutePath();
    }

}

